/**
 * Visual Blocks Language
 *
 * Copyright 2012 Google Inc.
 * http://blockly.googlecode.com/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @fileoverview Generating Python for math blocks.
 * @author gasolin@gmail.com  (Fred Lin)
 */
'use strict';

goog.provide('Blockly.Python.math');

goog.require('Blockly.Python');


Blockly.Python.math_whole_number = function() {
  // Numeric value.
  var code = window.parseFloat(this.getFieldValue('NUM'));
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Python.ORDER_UNARY_SIGN : Blockly.Python.ORDER_ATOMIC;
  return [code, order];
};


Blockly.Python.math_positive_number = function() {
  // Numeric value.
  var code = window.parseFloat(this.getFieldValue('NUM'));
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Python.ORDER_UNARY_SIGN : Blockly.Python.ORDER_ATOMIC;
  return [code, order];
};
Blockly.Python.math_angle = function() {
  // Numeric value.
  var code = window.parseFloat(this.getFieldValue('NUM'));
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Python.ORDER_UNARY_SIGN : Blockly.Python.ORDER_ATOMIC;
  return [code, order];
};

Blockly.Python.math_number = function() {
  // Numeric value.
  var code = window.parseFloat(this.getFieldValue('NUM'));
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Python.ORDER_UNARY_SIGN : Blockly.Python.ORDER_ATOMIC;
  return [code, order];
};



Blockly.Python.math_show_number = function() {
  // Numeric value.
  var code = Blockly.Python.valueToCode(this, 'number_values',Blockly.Python.ORDER_UNARY_SIGN) || 0;
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Python.ORDER_UNARY_SIGN : Blockly.Python.ORDER_ATOMIC;
  return [code, order];
};


Blockly.Python.operator_add = function() {
  var argument0 = Blockly.Python.valueToCode(this, 'NUM1',Blockly.Python.ORDER_ADDITIVE) || '0';
  var argument1 = Blockly.Python.valueToCode(this, 'NUM2',Blockly.Python.ORDER_ADDITIVE) || '0';
  var code = argument0+'+'+argument1;
  return [code,Blockly.Python.ORDER_ADDITIVE];
};


Blockly.Python.operator_subtract = function() {
  var argument0 = Blockly.Python.valueToCode(this, 'NUM1',Blockly.Python.ORDER_ADDITIVE) || '0';
  var argument1 = Blockly.Python.valueToCode(this, 'NUM2',Blockly.Python.ORDER_ADDITIVE) || '0';
  var code = argument0+'-'+argument1;
  return [code,Blockly.Python.ORDER_ADDITIVE];
};


Blockly.Python.operator_multiply = function() {
  var argument0 = Blockly.Python.valueToCode(this, 'NUM1',Blockly.Python.ORDER_MULTIPLICATIVE) || '0';
  var argument1 = Blockly.Python.valueToCode(this, 'NUM2',Blockly.Python.ORDER_MULTIPLICATIVE) || '0';
  var code = argument0+'*'+argument1;
  return [code,Blockly.Python.ORDER_MULTIPLICATIVE];
};


Blockly.Python.operator_divide = function() {
  var argument0 = Blockly.Python.valueToCode(this, 'NUM1',Blockly.Python.ORDER_MULTIPLICATIVE) || '0';
  var argument1 = Blockly.Python.valueToCode(this, 'NUM2',Blockly.Python.ORDER_MULTIPLICATIVE) || '0';
  var code = argument0+'/'+argument1;
  return [code,Blockly.Python.ORDER_MULTIPLICATIVE];
};


Blockly.Python.operator_mapping = function() {
  var value_map_value = Blockly.Python.valueToCode(this, 'map_value', Blockly.Python.ORDER_ATOMIC);
  var value_map_top = Blockly.Python.valueToCode(this, 'map_top', Blockly.Python.ORDER_ATOMIC);
  var value_map_bottom = Blockly.Python.valueToCode(this, 'map_bottom', Blockly.Python.ORDER_ATOMIC);
  var value_to_top = Blockly.Python.valueToCode(this, 'to_top', Blockly.Python.ORDER_ATOMIC);
  var value_to_bottom = Blockly.Python.valueToCode(this, 'to_bottom', Blockly.Python.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
 var scale = function() {
    return Blockly.Python.provideFunction_(
        'scale',
        ['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ +
            '(val, src, dst):',
         '  value=((val - src[0]) / (src[1]-src[0])) * (dst[1]-dst[0]) + dst[0]',
         "  value=float('%.2f' % value)",
         '  return value']);
  };

  var code=scale()+'(' + value_map_value + ', (' + value_map_top+ ','+value_map_bottom+'),(' + value_to_top +','+value_to_bottom+ '))';
  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.Python.ORDER_STRING_CONVERSION];
};

Blockly.Python.operator_random = function() {
  var argument0 = Blockly.Python.valueToCode(this, 'FROM',Blockly.Python.ORDER_ATOMIC) || '0';
  var argument1 = Blockly.Python.valueToCode(this, 'TO',Blockly.Python.ORDER_ATOMIC) || '0';
  var code = 'random('+argument0+','+ argument1+')';
  return [code,Blockly.Python.ORDER_NONE];
};
